﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using PairingMatrix.Model;
using System.Linq;

namespace PairingMatrix.ViewModel
{
    public class PairViewModel : ViewModelBase, IPair
    {
        private readonly PairModel _model;

        public PairViewModel(PairModel model)
        {
            _model = model;

            _cardNumbers = new ObservableCollection<IProjectCardViewModel>();
            _model.Cards.ToList().ForEach(c => _cardNumbers.Add(new ProjectCardViewModel(c)));
            _model.Cards.CollectionChanged += CardNumbers_CollectionChanged;

            _teamMembers = new ObservableCollection<ITeamMemberCardViewModel>();
            _model.Members.ToList().ForEach(m => _teamMembers.Add(new TeamMemberCardViewModel(m)));
            _model.Members.CollectionChanged += Members_CollectionChanged;
        }

        void Members_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                var newItem = (TeamMemberModel)e.NewItems[0];
                _teamMembers.Add(new TeamMemberCardViewModel(newItem));
            }
        }

        void CardNumbers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if(e.Action == NotifyCollectionChangedAction.Add)
            {
                var newItem = (ProjectCardModel)e.NewItems[0];
                _cardNumbers.Add(new ProjectCardViewModel(newItem));
            }
        }

        private ObservableCollection<ITeamMemberCardViewModel> _teamMembers;
        public ObservableCollection<ITeamMemberCardViewModel> TeamMembers
        {
            get { return _teamMembers; }
        }

        private ObservableCollection<IProjectCardViewModel> _cardNumbers;
        public ObservableCollection<IProjectCardViewModel> CardNumbers
        {
            get { return _cardNumbers; }
        }
    }
}